package kr.zetaomicron.ultrasolitaire;

import android.util.Log;

class SelectCard {
	private static final int MAX_CARDS = 23;

	private boolean mValid;
	private int mSelected;
	private Card[] mCard;
	private int mCardCount;
	private CardAnchor mCardAnchor;
	private int mCardAnchorIdx;
	private float mLeftEdge;
	private float mRightEdge;
	private int mHeight;

	public SelectCard() {
		mHeight = 1;
		mCard = new Card[MAX_CARDS];
		Clear();
	}

	private void Clear() {
		mValid = false;
		mSelected = -1;
		mCardCount = 0;
		mLeftEdge = -1;
		mRightEdge = -1;
		mCardAnchor = null;
		mCardAnchorIdx = -1;	// invalid index
		for (int i = 0; i < MAX_CARDS; i++) {
			mCard[i] = null;
		}
	}

  public void SetHeight(int height) { mHeight = height; }
  public boolean IsValid() { return mValid; }
	public Card[] GetCards() {	return mCard;	}
	public CardAnchor GetAnchor() { return mCardAnchor; }
	public int GetCardAnchorIdx() {	return mCardAnchorIdx;	}
	public int GetFullCount() {	return mCardCount;	}
	public int GetCount() {
		if (mSelected == -1)
			return mCardCount;
		return mCardCount - mSelected;
	}

//  public void Draw(DrawMaster drawMaster, Canvas canvas) {
//    drawMaster.DrawLightShade(canvas);
//    for (int i = 0; i < mCardCount; i++) {
//      drawMaster.DrawCard(canvas, mCard[i]);
//    }
//  }

  	public void InitFromAnchor(CardAnchor cardAnchor, int idx) {
  		mValid = true;
  		mSelected = -1;
  		mCardAnchor = cardAnchor;
  		mCardAnchorIdx = idx;
  		Card[] card = cardAnchor.GetCardStack();
  		for (int i = 0; i < card.length; i++) {
  			mCard[i] = card[i];
  		}
  		mCardCount = card.length;

  		int mid = mCardCount / 2;
  		if (mCardCount % 2 == 0) {
  			mid--;
  		}
  		float x = mCard[0].GetX(); 
  		float y = mCard[mid].GetY();
  		if (x - mid * (Card.WIDTH + 5) < 0) {
  			mid = 0;
  			x = Settings.WORLD_WIDTH - 5;
  		}

		for (int i = 0; i < mCardCount; i++) {
			mCard[i].SetPosition(x - (i - mid) * (Card.WIDTH + 5), y);
		}

		mLeftEdge = cardAnchor.GetLeftEdge();
		mRightEdge = cardAnchor.GetRightEdge();
  	}

	public boolean Tap(float x, float y) {
		float left = mLeftEdge == -1 ? mCard[0].GetY() + Card.HEIGHT / 2 : mLeftEdge;
		float right = mRightEdge == -1 ? mCard[0].GetY() - Card.HEIGHT / 2 : mRightEdge;
		mSelected = -1;
		if (y <= left && y >= right) {
			for (int i = 0; i < mCardCount; i++) {
				if (x >= mCard[i].GetX() - Card.WIDTH / 2 && x <= mCard[i].GetX() + Card.WIDTH / 2) {
					mSelected = i;
					return true;
				}
			}
		}
		return false;
	}

  public void Release() {
    if (mValid) {
      mValid = false;
      for (int i = 0; i < mCardCount; i++) {
        mCardAnchor.AddCard(mCard[i]);
      }
      Clear();
    }
  }

  public Card[] DumpCards() {
    Card[] ret = null;
    if (mValid) {
      mValid = false;
      if (mSelected > 0) {
        for (int i = 0; i < mCardCount; i++) {
          if (i < mSelected) {
            mCardAnchor.AddCard(mCard[i]);
          } else if (i == mSelected) {
            for (int j = 0; i < mCardCount; i++, j++) {
              mCard[j] = mCard[i];
            }
            break;
          }
        }
      }

      ret = new Card[GetCount()];
      for (int i = 0; i < GetCount(); i++) {
        ret[i] = mCard[i];
      }
      Clear();
    }
    return ret;
  }

//  public void Scroll(float dy) {
//    float x, y;
//    for (int i = 0; i < mCardCount; i++) {
//      x = mCard[i].GetX();
//      y = mCard[i].GetY() - dy;
//      mCard[i].SetPosition(x, y);
//    }
//  }
  
	public void Scroll(float dx) {
		float x, y;
		for (int i = 0; i < mCardCount; i++) {
			x = mCard[i].GetX() + dx;
			y = mCard[i].GetY();
			mCard[i].SetPosition(x, y);
		}
	}

	public boolean IsOnCard() { return mSelected != -1; } 
}